<?php
  class ProvinceService{
    	
  	function getProvinceById($uid, $provinceAccessId, $districtAccessId, $tambonAccessId){
		$where = array();
		foreach($provinceAccessId as $id){
			$where[] = " pn_degis_contact.aco_province_id = ".$id;
		}
		$whereImplode = implode(' OR ', $where);
		
		$sql = "
             SELECT 
              pn_degis_province.apv_id,
              pn_degis_province.apv_name,
              pn_degis_province.apv_lat,
              pn_degis_province.apv_lon,
              COUNT(pn_degis_contact.aco_province_id) AS count_population, 
              MAX(pn_degis_contact.contact_lu_date) 
             FROM 
              samuidemap.pn_degis_province,
              samuidemap.pn_degis_contact
            WHERE  
            ( ".
              $whereImplode
            .")
            AND 
              pn_degis_contact.aco_province_id = pn_degis_province.apv_id 
                    
            GROUP BY 
              pn_degis_contact.aco_province_id
            ORDER BY 
              pn_degis_province.apv_id ";
//echo $sql; exit;
			$column = array('id', 'name', 'lat', 'lon','count_population', 'update');
			$result = DBUtil::executeSQL($sql);
			$objectArray = DBUtil::marshallObjects ($result, $column);
			
			return $objectArray; 
    	//$this->setResponse($uid, $provinceIdArray, $districtIdArray, $tambonIdArray, $objectArray);
  	}
  	
  	function arrayToXML($uid, $uname, $provinceAccessId, $districtAccessId, $tambonAccessId, $objectArray){
  		//print_r($objectArray); exit;
      $accessArray['province_access'] = implode(",", $provinceAccessId);
      $accessArray['district_access'] = implode(",", $districtAccessId);                              
      $accessArray['tambon_access'] = implode(",", $tambonAccessId);
     
      //Gen xml
      $xml = new DOMDocument('1.0', 'UTF-8');
      $xml->formatOutput = true;
      $xml_node_datas = $xml->createElement("datas");
      $xml->appendChild( $xml_node_datas );
      foreach($objectArray as $val){
        $provinceId = "pv".$val[id];
        //echo $provinceId; exit;
        $xml_node_data = $xml->createElement("data");
        $xml_node_datas->appendChild( $xml_node_data );
        $xml_node_data->setAttribute("id", $provinceId);
          $xml_node_title = $xml->createElement("title",$val[name]);
          $xml_node_data->appendChild( $xml_node_title );
          $xml_node_country = $xml->createElement("country","ประเทศไทย");
          $xml_node_data->appendChild( $xml_node_country );
          $xml_node_logo = $xml->createElement("logo","http://www.votethailand.com/images/votethailandlogo.png");
          $xml_node_data->appendChild( $xml_node_logo );
          $xml_node_population = $xml->createElement("population",$val[count_population]);
          $xml_node_data->appendChild( $xml_node_population );
          $xml_node_latitude = $xml->createElement("latitude",$val[lat]);
          $xml_node_data->appendChild( $xml_node_latitude );
          $xml_node_longitude = $xml->createElement("longitude",$val[lon]);
          $xml_node_data->appendChild( $xml_node_longitude );
          $xml_node_span = $xml->createElement("span","0.4");
          $xml_node_data->appendChild( $xml_node_span );
          $xml_node_infopath = $xml->createElement("infopath","information.xml");
          $xml_node_data->appendChild( $xml_node_infopath );
          $xml_node_reportpath = $xml->createElement("reportpath","http://data.votethailand.com/report_no_data.xml");
          $xml_node_data->appendChild( $xml_node_reportpath );
          $xml_node_xmlcurrent = $xml->createElement("xmlcurrent","pv.xml");
          $xml_node_data->appendChild( $xml_node_xmlcurrent );
               
          $nextxml = "http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']."?module=VoteDataCenter&amp;type=service&amp;func=district&amp;province=".$accessArray['province_access']."&amp;district=".$accessArray['district_access']."&amp;tambon=".$accessArray['tambon_access']."&amp;uid=".$uid."&amp;sp=".$val[id];
          //echo $nextxml;

          $xml_node_xmlpath = $xml->createElement("xmlpath",$nextxml);//next xml
          $xml_node_data->appendChild( $xml_node_xmlpath );
          $xml_node_xmlupdate = $xml->createElement("xmlupdate","ProvinceData");
          $xml_node_data->appendChild( $xml_node_xmlupdate );
          $xml_node_date = $xml->createElement("date",$val[update]);
          $xml_node_data->appendChild( $xml_node_date );
          
					$zippath = "http://".$_SERVER['SERVER_NAME']."/xml/".$uname."-".$uid.".zip";
					$xml_node_zippath = $xml->createElement("zippath", $zippath);
					$xml_node_data->appendChild( $xml_node_zippath );		          
      }

			return $xml;  		
  	}

  	function renderXMLResponse($xmlObject){
      echo $xmlObject->saveXML();
  	}
  		      				        	
  	function saveXMLResponse($xmlDirectoryPath, $xmlFileName, $xmlObject){      
      $xmlObject->save($xmlDirectoryPath.$xmlFileName);	
  	}  
  	
  }
?>